package com.gdssoft.pdaweb.jobs;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.pdaweb.model.AutoPlan;
import com.gdssoft.pdaweb.service.AutoPlanService;

public class AutoPlanExecutorJob {
	
	private transient final Log logger = LogFactory.getLog(getClass());
	
	private AutoPlanService autoPlanService;
	
	public void setAutoPlanService(AutoPlanService autoPlanService) {
		this.autoPlanService = autoPlanService;
	}
	
	public void doJob() {
		logger.info("AutoPlanExecutorJob start -------------------");
		execAmAutoPlan(); // 电
		logger.info("-------- 黄金分割 ----------------------------");
		execWaterAutoPlan(); // 水
		logger.info("AutoPlanExecutorJob end ---------------------");
	}
	private void execAmAutoPlan() {
		List<AutoPlan> autoPlans = autoPlanService.listAmAutoPlanToday();
		
		logger.info("自动执行的计划数：" + autoPlans.size());
		
		for (AutoPlan autoPlan : autoPlans) {
			try {
				autoPlanService.executePlan(autoPlan);
				logger.info("-- 编号：" + autoPlan.getId() + " -- OK");
			} catch (Exception e) {
				logger.error("计划执行失败，ID：" + autoPlan.getId(), e);
			}
		}
	}
	private void execWaterAutoPlan() {
		List<AutoPlan> autoPlans = autoPlanService.listWaterAutoPlanToday();
		
		logger.info("自动执行的计划数：" + autoPlans.size());
		
		for (AutoPlan autoPlan : autoPlans) {
			try {
				autoPlanService.executePlan(autoPlan);
				logger.info("-- 编号：" + autoPlan.getId() + " -- OK");
			} catch (Exception e) {
				logger.error("计划执行失败，ID：" + autoPlan.getId(), e);
			}
		}
	}
}
